package kattmoy.lucene.text;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * 写入索引
 * @author kattomy
 *
 */
public class First {

    public static void main(String[] args) throws IOException {
    	
    	//数据来源
    	File dataDir = new File("D:\\text\\");
    	File[] dataFiles = dataDir.listFiles();
    	
    	//分词器
        Analyzer a = new StandardAnalyzer();
        //索引文档的存储位置 FSDirectory将索引写入硬盘，RAMDirectory将索引写入内存
        Directory dir = FSDirectory.open(Paths.get("./index"));
        //操作索引库的配置信息，传入分词器
        IndexWriterConfig iwc = new IndexWriterConfig(a);
        //建立索引的核心类，用来操作索引（增、删、改），传入索引文档存储位置以及操作索引库的配置信息
        IndexWriter iw = new IndexWriter(dir, iwc);
        //Document索引文档
//        Document doc = new Document();
        for(int i = 0; i < dataFiles.length; i++){ 
            if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){
                 System.out.println("正在写入文件" + dataFiles[i].getCanonicalPath() + "的索引"); 
                 Document document = new Document(); 
                 Reader txtReader = new FileReader(dataFiles[i]); 

                 BufferedReader r = new BufferedReader(txtReader);
                 StringBuilder b = new StringBuilder();
                 String line;
                 while((line=r.readLine())!=null) {
                   b.append(line);
                   b.append("\r\n");
                 }
                 
                 System.out.println(b.toString());
                 
                 document.add(new TextField("path", dataFiles[i].getCanonicalPath(), Field.Store.YES));
                 document.add(new TextField("contents", b.toString(), Field.Store.YES));
//               document.add(Field.Text("path",dataFiles[i].getCanonicalPath())); 
//               document.add(new TextField("contents",txtReader)); 
                 iw.addDocument(document); 
            } 
       } 
//        doc.add(new TextField("info", "this is my first lucene test", Field.Store.YES));
//        iw.addDocument(doc);
        iw.close();
        dir.close();
    }

}